package com.wn.class30.service.impl;

import com.wn.class30.entity.*;
import com.wn.class30.mapper.*;
import com.wn.class30.service.UserService;
import com.wn.class30.tools.UserIp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserServiceImp implements UserService {
    @Resource
    private UserDao userDao;
    @Resource
    private VipDao vipDao;
    @Resource
    private UserCuponDao userCuponDao;
    @Resource
    private MealDao mealDao;

    @Override
    /*用户关注公众号*/
    public void insertUser(User user) throws Exception {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andOpenidEqualTo(user.getOpenid());
        List<User> users = userDao.selectByExample(userExample);
        log.debug("users="+String.valueOf(users));
        if(users==null || users.size()==0){
            int insert = userDao.insertSelective(user);
            log.debug("关注成功");
        }else{
            user.setUserId(users.get(0).getUserId());
            userDao.updateByPrimaryKeySelective(user);
            log.debug("重新关注成功");
        }
    }
    /*用户取关公众号*/
    @Override
    public void updateUser(User user) throws Exception {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andOpenidEqualTo(user.getOpenid());
        List<User> users = userDao.selectByExample(userExample);
        if(users==null || users.size()==0){
            log.debug("数据库数据丢失");
        }else{
        user.setUserId(users.get(0).getUserId());
        userDao.updateByPrimaryKeySelective(user);}
    }

    @Override
    public List<User> queryByOpenid(String openid) throws Exception {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andOpenidEqualTo(openid);
        List<User> users = userDao.selectByExample(userExample);
        return users;
    }

    @Override
    public Vip queryLvNameByUserid(Integer userid) throws Exception {
        User user = userDao.selectByPrimaryKey(userid);
        Vip vip = vipDao.selectByPrimaryKey(user.getLvId());
        return vip;
    }

    @Override
    public List<UserCupon> queryUserCuponByUserid(Integer userid) throws Exception {
        UserCuponExample userCuponExample = new UserCuponExample();
        UserCuponExample.Criteria criteria = userCuponExample.createCriteria();
        criteria.andUserIdEqualTo(userid);
        List<UserCupon> userCupons = userCuponDao.selectByExample(userCuponExample);
        return userCupons;
    }
    //查询所有vip套餐
    @Override
    public List<Meal> queryMeal() {
        return mealDao.selectByExample(null);
    }
    //根据用户id查询用户信息
    @Override
    public User queryUserByUderid(Integer userId) {
        return userDao.selectByPrimaryKey(userId);
    }
    //查询所有vip等级

    @Override
    public List<Vip> queryAllVip() {
        return vipDao.selectByExample(null);
    }

    //查询用户和其对应的优惠券,三表联立
    @Override
    public List<Map<String, Object>> queryUserCupon2(Integer userid) {
        List<Map<String, Object>> maps = userDao.selectMap(userid);
        for (Map<String, Object> map : maps) {
            //格式化日期
            Date cupon_gettime = (Date) map.get("cupon_gettime");
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
            map.put("cupon_gettime",sd.format(cupon_gettime));
        }
        return maps;
    }

    @Override
    public void updatePointsByUserid(Integer userid, Integer points) {
        User user = userDao.selectByPrimaryKey(userid);
        user.setPoints(user.getPoints()+points);
        userDao.updateByPrimaryKeySelective(user);
    }

    @Override
    public void updatePhone(Integer uid,String phone) {
        User user = new User();
        user.setUserId(uid);
        user.setTel(phone);
        userDao.updateByPrimaryKeySelective(user);
    }

    @Override
    public List<Map<String,Object>> selectMealAndVip(Integer mealId) {
        List<Map<String,Object>> mealAndVip = userDao.selectMealAndVip(mealId);
        return mealAndVip;
    }

    @Override
    public void updateUserByMealAndVip(Integer userid, Map<String, Object> mealAndVip) {
        User user = userDao.selectByPrimaryKey(userid);
        user.setPoints(user.getPoints()+(int)mealAndVip.get("lv_points"));
//        user.setLvId((int)mealAndVip.get("lv_id"));
        userDao.updateByPrimaryKeySelective(user);
    }

    @Resource
    MealPayDao mealPayDao;
    @Override
    public MealPay insertMealPay(List<Map<String, Object>> mealAndVip, User user) {
        MealPay mealPay = null;
        if(mealAndVip!=null){
            mealPay = new MealPay();
            mealPay.setUsertel(user.getTel());
            mealPay.setUserid(user.getUserId());
            mealPay.setMealName((String) mealAndVip.get(0).get("meal_name"));
            mealPay.setMealId((Integer) mealAndVip.get(0).get("meal_id"));
            mealPay.setMealMoney((Double) mealAndVip.get(0).get("meal_money"));
            mealPay.setVipOrderNum(UserIp.getOrderIdByTime());
            System.out.println(mealPay.getVipOrderNum()+"============================");
            mealPay.setPayTime(new Date());
            mealPay.setMealpayStatus("0");
        }
        mealPayDao.insert(mealPay);
        return mealPay;
    }

    @Override
    public String checkPhoneExist(Integer uid) {
        User user = userDao.selectByPrimaryKey(uid);
        return user.getTel();
    }
}
